package safeCommitter.core.preferences;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import safeCommitter.core.template.Activator;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>,
 * we can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 */

public class SamplePreferencePage extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {

	private StringFieldEditor testNGPath;
	private Composite parent;
	private BooleanFieldEditor performSearch;
	private BooleanFieldEditor addNGXml;

	public SamplePreferencePage() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("SafeCommitter properties");
	}

	/**
	 * Creates the field editors. Field editors are abstractions of the common
	 * GUI blocks needed to manipulate various types of preferences. Each field
	 * editor knows how to save and restore itself.
	 */
	@Override
	public void createFieldEditors() {
		parent = getFieldEditorParent();
		addField(new BooleanFieldEditor(
				SCPreferenceConstants.P_ALLOW_BLANK_COMMENTS,
				"Allow blank comments", parent));
		performSearch = new BooleanFieldEditor(
				SCPreferenceConstants.P_PERFORM_SEARCH, "Perform Search",
				parent);
		addField(performSearch);
		addNGXml = new BooleanFieldEditor(
				SCPreferenceConstants.P_ADD_TESTNG_XML,
				"Always Run the following testNG xmls (comma separated list)",
				parent);
		addField(addNGXml);
		testNGPath = new StringFieldEditor(
				SCPreferenceConstants.P_TESTNG_XML_PATH, "TestNG Xml files",
				parent);
		addField(testNGPath);
		testNGPath.setEnabled(Activator.getDefault().getPreferenceStore()
				.getBoolean(SCPreferenceConstants.P_ADD_TESTNG_XML), parent);
		performSearch.setPropertyChangeListener(this);
		addNGXml.setPropertyChangeListener(this);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(final IWorkbench workbench) {
	}

	@Override
	public void propertyChange(final PropertyChangeEvent event) {
		if (event.getSource().equals(addNGXml)) {
			testNGPath.setEnabled((Boolean) event.getNewValue(), parent);
		}
	}

}